#include <iostream>
#include <string>
#include <unordered_map>
#include "Protocol.hpp"
#include "Log.hpp"
using namespace std;
class Translation
{
public:
    Translation()
    {
    }
    Respond Execute(Request& req)
    {
        // Respond resp;
        // string English = req.GetEnglish();
        // while(!English.empty() && (English.back() == '\r' || English.back() == '\n'))
        // {
        //     English.pop_back();
        // }

        // string LowerEnglish = English;
        // for (char &c : LowerEnglish)
        //     c = tolower(c);

        // auto it = _dictionary.find(LowerEnglish); // 使用小写版本查询
        // if (it == _dictionary.end())
        // {
        //     resp.SetChinese("字典不存在该单词");
        //     return resp;
        // }
        // resp.SetChinese(it->second);
        // return resp; // 直接返回迭代器值避免二次查找

        Respond resp;
        string English = req.GetEnglish();
        
        // 移除首尾空白字符
        size_t start = English.find_first_not_of(" \t\r\n");
        size_t end = English.find_last_not_of(" \t\r\n");
        if (start != string::npos && end != string::npos) {
            English = English.substr(start, end - start + 1);
        }

        string LowerEnglish;
        for (char c : English) {
            LowerEnglish += tolower(c);
        }

        auto it = _dictionary.find(LowerEnglish);
        if (it == _dictionary.end()) {
            resp.SetChinese("未知单词: " + English);
        } else {
            resp.SetChinese(it->second);
        }
        return resp;
    }
    ~Translation()
    {
    }

private:
    unordered_map<string, string> _dictionary = {{"hello", "你好"},
                                                 {"world", "世界"},
                                                 {"python", "蟒蛇"},
                                                 {"c++", "C加加"},
                                                 {"server", "服务器"},
                                                 {"client", "客户端"},
                                                 {"good", "好"},
                                                 {"morning", "早晨"},
                                                 {"thank", "谢谢"},
                                                 {"you", "你"}};
};